package com.sls.web.manager.action.game;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;

import com.sls.core.common.CommonUtil;
import com.sls.web.manager.action.SuperAction;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.common.FilterStringSpaces;
import com.sls.web.manager.common.WebTools;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.Goods;
import com.sls.web.manager.game.pojo.GoodsBrand;
import com.sls.web.manager.game.pojo.GoodsSupply;
import com.sls.web.manager.game.pojo.NutsNet;
import com.sls.web.manager.game.pojo.OhFly;
import com.sls.web.manager.game.pojo.Tencent;
import com.sls.web.manager.game.service.AuditService;
import com.sls.web.manager.game.service.GoodsBrandService;
import com.sls.web.manager.game.service.GoodsService;
import com.sls.web.manager.game.service.NutsNetService;
import com.sls.web.manager.game.service.OhFlyService;
import com.sls.web.manager.game.service.TencentService;

/**
 * 商品审核
 * 
 * @author cx
 * 
 */
public class AuditAction extends SuperAction {

	private static final long serialVersionUID = -6739557007500528760L;
	@Resource(name = "auditService")
	private AuditService auditService;
	@Resource(name = "nutsNetService")
	private NutsNetService nutsNetService;
	@Resource(name = "ohFlyService")
	private OhFlyService ohFlyService;
	@Resource(name = "tencentService")
	private TencentService tencentService;
	@Resource(name = "goodsBrandSerice")
	private GoodsBrandService goodsBrandSerice;
	@Resource(name = "goodsService")
	private GoodsService goodsService;
	private List<GoodsBrand> brandList;
	private GoodsBrand brand;
	private String supplyId;
	private String originalGoodsId;
	private NutsNet nutsNet;
	private OhFly ohFly;
	private Tencent tencent;
	private String goodsCategoryId;
	private List<Goods> goodsList;
	private Goods goods;
	private String goodsId;
	private String goodsState;

	public AuditAction() {
	}

	/**
	 * 新增
	 */
	public String add() {
		return INPUT;
	}

	/**
	 * 列表
	 * 
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		try {
			if (StringUtils.isNotEmpty(originalGoodsId)) {
				if (Constants.NUTS_NET.equals(supplyId)) {
					nutsNet = nutsNetService.queryNutsNetById(originalGoodsId,
							getCurrentOperator());
				}
				if (Constants.OH_FLY.equals(supplyId)) {
					ohFly = ohFlyService.queryDetailById(originalGoodsId,
							getCurrentOperator());
				}
				if (Constants.TENCENT.equals(supplyId)) {
					tencent = tencentService.queryTencentByGoodsId(
							originalGoodsId, getCurrentOperator());
				}
				if (brandList == null) {

					brandList = auditService.queryBrandList(originalGoodsId,
							supplyId, getCurrentOperator());
				}
			}
		} catch (ManagerException e) {
			addActionError(e.getMessage());
			if (Constants.OH_FLY.equals(supplyId)) {
				flag = false;
				return "audit_success";
			}
		}
		return LIST;
	}

	/**
	 * 新增品牌
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveBrand() throws Exception {
		try {
			GoodsBrand goodsBrand = new GoodsBrand();
			goodsBrand.setSupplyId(supplyId);
			goodsBrand.setOriginalGoodsId(originalGoodsId);
			goodsBrand.setRemark("新增品牌");
			goodsBrand.setIsDelete("1");
			String brandId = auditService.addBrand(goodsBrand,
					getCurrentOperator());
			if (StringUtils.isNotEmpty(brandId)) {
				brand = goodsBrandSerice.queryGoodsBrandById(brandId,
						getCurrentOperator());
			}
			if (Constants.NUTS_NET.equals(supplyId)) {
				nutsNet = nutsNetService.queryNutsNetById(originalGoodsId,
						getCurrentOperator());
			}
			if (Constants.OH_FLY.equals(supplyId)) {
				ohFly = ohFlyService.queryDetailById(originalGoodsId,
						getCurrentOperator());
			}
			if (Constants.TENCENT.equals(supplyId)) {
				tencent = tencentService.queryTencentByGoodsId(originalGoodsId,
						getCurrentOperator());
			}
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
			return "audit_success";
		}
		return LIST;
	}

	/**
	 * 保存商品信息
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveGoods() throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String errorMsg = "";
		String id = "";
		try {
			String commonSalePrice = WebTools.getParameter("commonSalePrice");
			try {
				goods.setCommonSalePrice((int) (Double
						.parseDouble(commonSalePrice) * 1000));
			} catch (Exception e) {
				errorMsg = "格式转换异常!";
				return "ajax";
			}
			goods.setSupplyId(supplyId);
			String goodsCategoryId = goods.getGoodsCategoryId();
			goodsCategoryId = FilterStringSpaces
					.checkStringHasSpace(goodsCategoryId);
			goods.setGoodsCategoryId(goodsCategoryId);
			goods.setOriginalGoodsId(originalGoodsId);
			goodsId = auditService.addGoods(goods, getCurrentOperator());
			if (StringUtils.isEmpty(goodsId)) {
				return ERROR;
			}
			goods = goodsService.queryGoodsById(goodsId, getCurrentOperator());
			id = goodsId;
		} catch (ManagerException e) {
			errorMsg = e.getMessage();
		}
		resultMap.put("id", id);
		resultMap.put("errorMsg", errorMsg);
		resultMap.put("goods", goods);
		message = CommonUtil.gson().toJson(resultMap);
		return "ajax";
	}

	/**
	 * 保存商品到商品供应表中
	 * 
	 * @return
	 * @throws Exception
	 */
	public String saveGoodSupply() throws Exception {
		try {
			GoodsSupply goodsSupply = new GoodsSupply();
			goodsSupply.setSuppliersId(supplyId);
			goodsSupply.setOriginalGoodsId(originalGoodsId);
			if (StringUtils.isNotEmpty(goodsId)) {
				goodsSupply.setGoodsId(goodsId.trim());
			}
			GoodsSupply sup = auditService.addGoodsSupply(goodsSupply,
					getCurrentOperator());
			if (Constants.NUTS_NET.equals(supplyId)) {
				redirectionUrl = "nuts_net!list.htm";
			}
			if (Constants.OH_FLY.equals(supplyId)) {
				redirectionUrl = "oh_fly!list.htm";
			}
			if (Constants.TENCENT.equals(supplyId)) {
				redirectionUrl = "tencent!list.htm";
			}
			if (sup != null) {
				if ("0".equals(sup.getResCode())) {
					addActionMessage(sup.getResDescription());
					return "audit_update";
				}
			}
		} catch (ManagerException e) {
			flag = false;
			addActionError(e.getMessage());
		}
		return "audit_success";
	}

	/**
	 * 查询商品品牌下的所有商品
	 * 
	 * @return
	 * @throws Exception
	 */
	public String query() throws Exception {
		try {
			if (goodsList == null && goodsCategoryId != null) {
				goodsList = auditService.queryGoodsListByBrandId(
						goodsCategoryId, goodsState, getCurrentOperator());
			}
		} catch (ManagerException e) {
			addActionError(e.getMessage());
		}
		message = CommonUtil.gson().toJson(goodsList);
		return "ajax";
	}

	public List<GoodsBrand> getBrandList() {
		return brandList;
	}

	public void setBrandList(List<GoodsBrand> brandList) {
		this.brandList = brandList;
	}

	public GoodsBrand getBrand() {
		return brand;
	}

	public void setBrand(GoodsBrand brand) {
		this.brand = brand;
	}

	public String getSupplyId() {
		return supplyId;
	}

	public void setSupplyId(String supplyId) {
		this.supplyId = supplyId;
	}

	public String getOriginalGoodsId() {
		return originalGoodsId;
	}

	public void setOriginalGoodsId(String originalGoodsId) {
		this.originalGoodsId = originalGoodsId;
	}

	public NutsNet getNutsNet() {
		return nutsNet;
	}

	public void setNutsNet(NutsNet nutsNet) {
		this.nutsNet = nutsNet;
	}

	public OhFly getOhFly() {
		return ohFly;
	}

	public void setOhFly(OhFly ohFly) {
		this.ohFly = ohFly;
	}

	public Tencent getTencent() {
		return tencent;
	}

	public void setTencent(Tencent tencent) {
		this.tencent = tencent;
	}

	public String getGoodsCategoryId() {
		return goodsCategoryId;
	}

	public void setGoodsCategoryId(String goodsCategoryId) {
		this.goodsCategoryId = goodsCategoryId;
	}

	public List<Goods> getGoodsList() {
		return goodsList;
	}

	public void setGoodsList(List<Goods> goodsList) {
		this.goodsList = goodsList;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public String getGoodsId() {
		return goodsId;
	}

	public void setGoodsId(String goodsId) {
		this.goodsId = goodsId;
	}

	public String getGoodsState() {
		return goodsState;
	}

	public void setGoodsState(String goodsState) {
		this.goodsState = goodsState;
	}
}
